﻿Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Seuss.EnterpriseLibrary.DataAccess
Imports Seuss.EnterpriseLibrary.BusinessLogic
Imports System.Web
Imports System.Data

Partial Class Consulta_AL_InfoAcad_NA
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Expires = -1
        Response.AddHeader("Pragma", "no-cache")
        Response.AddHeader("cache-control", "private")
        Response.CacheControl = "no-cache"
        If Not Page.IsPostBack Then
        
            If String.IsNullOrEmpty(Request.QueryString("nCurGruCodigo")) Then
                ' lnkRegresar_Top.HRef = "./AL_InfoAcad.aspx"
                'lnkRegresar_Bottom.HRef = "./AL_InfoAcad.aspx"
                Call CargarBoletaNotasActual(IdentityUser.UserCode)
            Else
                'lnkRegresar_Top.HRef = "./AL_InfoAcad_NA.aspx"
                'lnkRegresar_Bottom.HRef = "./AL_InfoAcad_NA.aspx"
                Call CargarNotasDetallado(Request.QueryString("nCurGruCodigo"))
            End If
            Using Seguridad As New BLL_Administracion
                Seguridad.Set_Transaccion_Insert(Nothing, 100401, IdentityUser.UserCode, My.Request.UserHostAddress.ToString, CrearXMLTransaccion("Consulta Pagina Movil", "/Pagina|" + My.Request.Url.Segments(My.Request.Url.Segments.Length - 1).ToString))
            End Using
        End If
    End Sub

    Private Sub CargarBoletaNotasActual(ByVal cPerCodigo As String)
      
        Dim ldr As New List(Of IDataReader)
        Dim DivPrincipal As New StringBuilder
       
        DivPrincipal.Append("<table width='100%'>")

        DivPrincipal.Append("<tr style='color:#FFFFFF'>")
        DivPrincipal.Append("   <td></td>")
        DivPrincipal.Append("   <td style='background-color:#666666;font-size:12px;'>Cic</td>")
        DivPrincipal.Append("   <td style='background-color:#666666;font-size:12px;width:100px;'>Asignatura</td>")
        DivPrincipal.Append("   <td style='background-color:#666666;font-size:12px;'>Créd</td>")
        DivPrincipal.Append("   <td style='background-color:#666666;font-size:12px;'>N1</td>")
        DivPrincipal.Append("   <td style='background-color:#666666;font-size:12px;'>N2</td>")
        DivPrincipal.Append("   <td style='background-color:#666666;font-size:12px;'>N3</td>")
        DivPrincipal.Append("   <td style='background-color:#666666;font-size:12px;'>Prom</td>")
        DivPrincipal.Append("</tr>")

        Using ObjPool As New PoolDbCommand()
            Dim ObjUsuario As New DAL_Kiosco(ObjPool.Database)
            ObjPool.AddDbCommand(ObjUsuario.Get_BoletaNotas_By_cPerCodigo(cPerCodigo), PoolDbCommand.PoolDbCommandResultType.Query)
            ObjPool.Pooling(Nothing, ldr, True)
            ObjUsuario = Nothing
            While ldr(0).Read
                DivPrincipal.Append("<tr width='100%'>")
                DivPrincipal.Append("   <td style='font-size:10px;'><a style=""text-decoration:none"" href=""./AL_InfoAcad_NA.aspx?nCurGruCodigo=" + ldr(0).Item("nCurGruCodigo").ToString + """ ><img src='../Imagenes/Ok.gif' border='0'/><a></td>")
                DivPrincipal.Append("   <td style='font-size:10px;'>" + ldr(0).Item("nCicCodigo").ToString + "</td>")
                DivPrincipal.Append("   <td style='font-size:10px;width:20%;'>" + ldr(0).Item("cAsiDescripcion").ToString + "</td>")
                DivPrincipal.Append("   <td style='font-size:10px;'>" + Split(ldr(0).Item("ncurdetcredito").ToString, ".", -1)(0) + "</td>")
                DivPrincipal.Append("   <td style='font-size:10px;'>" + ldr(0).Item("P1").ToString + "</td>")
                DivPrincipal.Append("   <td style='font-size:10px;'>" + ldr(0).Item("P2").ToString + "</td>")
                DivPrincipal.Append("   <td style='font-size:10px;'>" + ldr(0).Item("P3").ToString + "</td>")
                DivPrincipal.Append("   <td style='font-size:10px;'>" + ldr(0).Item("Promedio").ToString + "</td>")
                DivPrincipal.Append("</tr>")
            End While
        End Using

        DivPrincipal.Append("</table>")
        lblBoletaNotas.Text = DivPrincipal.ToString
    End Sub

    Private Sub CargarNotasDetallado(ByVal nCurGruCodigo As Integer)
        Lnk_Regresar_1.HRef = "./AL_InfoAcad_NA.aspx"
        Lnk_Regresar_2.HRef = "./AL_InfoAcad_NA.aspx"
        Dim vnCurGruCodigo As Integer
        vnCurGruCodigo = nCurGruCodigo
        MvPrincipal.ActiveViewIndex = 1
        Dim CodigoRegistro As String = String.Empty
        Dim CodigoMatricula As String = String.Empty
        Dim CodigoRegistroSec As String = String.Empty
        Dim Promedio As Integer
        Dim SiProm As Boolean
        Dim lrd As New List(Of IDataReader)
        Using ObjPool As New PoolDbCommand()
            Dim ObjMenu As New DAL_Kiosco(ObjPool.Database)
            ObjPool.AddDbCommand(ObjMenu.Get_Registro_By_nCurGruCodigo(CapturaCurGrupoBase(vnCurGruCodigo)), PoolDbCommand.PoolDbCommandResultType.Query)
            ObjPool.AddDbCommand(ObjMenu.Get_cDocCodigoMatricula_By_cPerCodigo_nCurGruCodigo(IdentityUser.UserCode, (vnCurGruCodigo)), PoolDbCommand.PoolDbCommandResultType.Query)
            ObjPool.AddDbCommand(ObjMenu.Get_Registro_By_nCurGruCodigo(vnCurGruCodigo), PoolDbCommand.PoolDbCommandResultType.Query)
            ObjPool.Pooling(Nothing, lrd, True)
            If lrd(0).Read Then
                CodigoRegistro = lrd(0).GetString(0)
            End If
            If lrd(1).Read Then
                CodigoMatricula = lrd(1).GetString(0)
            End If
            If lrd(2).Read Then
                CodigoRegistroSec = lrd(2).GetString(0)
            End If
        End Using
        lrd(0).Close()
        lrd(0) = Nothing
        lrd(1).Close()
        lrd(1) = Nothing
        lrd(2).Close()
        lrd(2) = Nothing
        Dim BodyNotas As String

        '******** PARCIALES ********
        Dim LdrPar As New List(Of IDataReader)
        Using ObjPool As New PoolDbCommand()
            Dim ObjMenu As New DAL_Kiosco(ObjPool.Database)
            ObjPool.AddDbCommand(ObjMenu.Get_Parciales_By_cDocCodigo(CodigoRegistro), PoolDbCommand.PoolDbCommandResultType.Query)
            ObjPool.Pooling(Nothing, LdrPar, True)
        End Using

        BodyNotas = "<table border=0 width=100% cellspacing=1 cellpadding=5>"
        Do While LdrPar(0).Read()
            Dim Notas As Boolean
            SiProm = False
            BodyNotas = BodyNotas + "<tr style='color:White;background:#004370'>"
            BodyNotas = BodyNotas + "<td width='150px' colspan='2' style='font-size:10px;'><b>" + CStr(LdrPar(0).Item("cConDescripcion")) + "</b></td>"
            BodyNotas = BodyNotas + "</tr>"

            Dim LdrNotas As New List(Of IDataReader)
            Using ObjPool As New PoolDbCommand()
                Dim ObjMenu As New DAL_Kiosco(ObjPool.Database)
                ObjPool.AddDbCommand(ObjMenu.Get_Notas_By_Registro_Matricula_Descripcion(CodigoRegistroSec, CodigoMatricula, CStr(LdrPar(0).Item("nSubPrdCodigo")), CodigoRegistro), PoolDbCommand.PoolDbCommandResultType.Query)
                ObjPool.Pooling(Nothing, LdrNotas, True)
                If LdrNotas(0).RecordsAffected <> 0 Then
                    Notas = True
                End If
                If Notas = True Then
                    BodyNotas = BodyNotas + "<tr><td><table CellSpacing=0 border=0 width=100% style='background:#EDF8FC'>"
                End If
                Do While LdrNotas(0).Read()
                    BodyNotas = BodyNotas + "<tr><td style='font-size:10px;text-align:left'>" + CStr(LdrNotas(0).GetString(2)) + " (" + CStr(LdrNotas(0).GetString(0)) + ") </td><td>" + CStr(LdrNotas(0).GetValue(1)) + "</td></tr>"
                    SiProm = True
                Loop
                If Notas = True Then
                    If SiProm = True Then
                        BodyNotas = BodyNotas + "<tr bgcolor=#BAE3F3><td><b>PROMEDIO</b></td>"
                        Promedio = CDec(MostrarPromedio(CodigoMatricula, vnCurGruCodigo, LdrPar(0).Item("nSubPrdCodigo")))
                        If Promedio <= 10 Then
                            BodyNotas = BodyNotas + "<td style='color:Red;text-align:center'><b>" + CStr(MostrarPromedio(CodigoMatricula, vnCurGruCodigo, LdrPar(0).Item("nSubPrdCodigo"))) + "<b></td>"
                        Else
                            BodyNotas = BodyNotas + "<td style='color:Blue;text-align:center;'><b>" + CStr(MostrarPromedio(CodigoMatricula, vnCurGruCodigo, LdrPar(0).Item("nSubPrdCodigo"))) + "<b></td>"
                        End If
                        BodyNotas = BodyNotas + "</tr><tr bgcolor=#BAE3F3><td><b>FÓRMULA</b></td><td><b>" + CStr(LdrPar(0).Item("cRegSubPrdFormula")) + "</b></td></tr>"
                    Else
                        BodyNotas = BodyNotas + "<tr><td>No hay notas registradas</td></tr>"
                    End If
                    BodyNotas = BodyNotas + "</tr></td></table>"
                End If
            End Using
            LdrNotas(0).Close()
            LdrNotas(0) = Nothing
        Loop
        BodyNotas = BodyNotas + "</Table><br/>"
        lblnotas.Text = BodyNotas
        LdrPar(0).Close()
        LdrPar(0) = Nothing
    End Sub

    Public Function CapturaCurGrupoBase(ByVal nCurGruCodigo As Long) As Long
        Dim VCurGrupoBase As Long = 0
        Dim ldr As New List(Of IDataReader)
        Using ObjPool As New PoolDbCommand()
            Dim ObjMenu As New DAL_Kiosco(ObjPool.Database)
            ObjPool.AddDbCommand(ObjMenu.Get_nCurGruCodigoBase_By_nCurgruCodigo(nCurGruCodigo), PoolDbCommand.PoolDbCommandResultType.Query)
            ObjPool.Pooling(Nothing, ldr, True)
            If ldr(0).Read Then
                VCurGrupoBase = ldr(0).GetInt32(0)
            Else
                VCurGrupoBase = nCurGruCodigo
            End If
        End Using
        ldr(0).Close()
        ldr(0) = Nothing
        Return VCurGrupoBase
    End Function

    Private Function MostrarPromedio(ByVal cdoccodigo As String, ByVal ncurgrucodigo As Long, ByVal nsubprdcodigo As Integer) As Double
        Dim PromedioParcial As Double = 0.0
        Dim LdrNotaP As New List(Of IDataReader)
        Using ObjPool As New PoolDbCommand()
            Dim ObjMenu As New DAL_Kiosco(ObjPool.Database)
            ObjPool.AddDbCommand(ObjMenu.Get_PromedioParcial_By_cDocCodigo_nCurGruCodigo_nSubPrdCodigo(cdoccodigo, ncurgrucodigo, nsubprdcodigo), PoolDbCommand.PoolDbCommandResultType.Query)
            ObjPool.Pooling(Nothing, LdrNotaP, True)
            If LdrNotaP(0).Read Then
                PromedioParcial = LdrNotaP(0).GetDouble(0)
            Else
                PromedioParcial = 0.0
            End If
            Return PromedioParcial
        End Using
    End Function


End Class
